package com.base.cn.platform.os.controller.user.login;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.rsa.WebRsaUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.common.utils.user.LoginForm;
import com.base.cn.platform.os.common.utils.user.UserExpandFrom;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import com.base.cn.platform.os.service.manage.user.login.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 用户登录注册接口
 *
 * @author s.li
 * @create 2018-08-27-10:36
 */
@RestController
@RequestMapping("/api/user")
public class ApiUserLoginController extends ApiBaseController {
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CustomCacheChannel customCacheChannel;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 验证找回密码的手机短信验证码
     * @param mobile 手机号
     * @param mobileCode 验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/verificationRetrieveMobileCode")
    public Map<String,Object> verificationRetrieveMobileCode(@RequestParam("mobile") String mobile,
                                                             @RequestParam("mobileCode") String mobileCode){
        return userLoginService.verificationForgetMobileCode(mobile,mobileCode);
    }

    /**
     * 找回密码
     * @param mobile 手机号
     * @param mobileCode 手机验证码
     * @param password 新密码
     * @param confirmPwd 确认密码
     * @return Map<String, Object>
     */
    @RequestMapping("/retrieveUpdatePassword")
    public Map<String, Object> retrieveAjaxPassword(@RequestParam("mobile") String mobile,
                                                    @RequestParam("mobileCode") String mobileCode,
                                                    @RequestParam("password") String password,
                                                    @RequestParam("confirmPwd") String confirmPwd,
                                                    @RequestParam(value = "rsaSign",required = false) String rsaSign) throws Exception {
        Map<String,Object> resultMap = userLoginService.retrievePwd(mobile,mobileCode,password,confirmPwd,rsaSign);
        if(!ResultUtil.getSuccess(resultMap)){//如果登录失败，则重新获取密钥
            Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
            resultMap.put("keyMap",keyMap);
        }
        return  resultMap;
    }

    /**
     * 查询专业数据
     * @return Map<String,Object>
     */
    @RequestMapping("/perfectSubject")
    public Map<String,Object> findLayeredSubjectList(){
        Map<String, Object> params = new HashMap<>();
        params.put("status",1);//查询状态正常的
        Map<String,List<Map<String,Object>>> subjectMapList = subjectService.findSubjectListMap(params);
        return ResultUtil.SUCCESS("专业获取成功",subjectMapList);
    }

    /**
     * 保存用户设置的专业
     * @param sessionId  登录、注册接口的返回的sessionId
     * @param parentId 选择的专业父ID
     * @param childIds 选择的专业子级ID串，多个ID间以“,”隔开，选择的子级专业必须是父ID的子级
     * @return Map<String,Object>
     */
    @RequestMapping("/savePerfectSubject")
    public Map<String,Object> savePerfectSubject(@RequestParam("sessionId") String sessionId,
                                                 @RequestParam("parentId") BigDecimal parentId,
                                                 @RequestParam("childIds") String childIds){
        Map<String,Object> resultMap = userLoginService.setUserSubjectIds(sessionId,parentId,childIds,"false",LoginForm.LOGIN_FORM_APP);
        return  resultMap;
    }


    /**
     * 完善用户资料，绑定手机号
     * @param sessionId 登录、注册接口的返回的sessionId
     * @param mobile 手机号
     * @param mobileCode 手机验证码
     * @return Map<String,Object>
     */
    @RequestMapping("/perfectTheData")
    public Map<String,Object> perfectTheData(@RequestParam("sessionId") String sessionId,
                                             @RequestParam("mobile") String mobile,
                                             @RequestParam("mobileCode") String mobileCode){
        Map<String,Object> resultMap = userLoginService.setUserMobile(sessionId,mobile,mobileCode,"false",LoginForm.LOGIN_FORM_APP);
        return resultMap;
    }

    /**
     * 学员用户注册接口
     * @param mobile 手机号
     * @param code 验证码
     * @param userPassword 用户密码
     * @return  Map<String,Object>
     */
    @RequestMapping("/register")
    public Map<String,Object> register(@RequestParam("mobile") String mobile,
                                       @RequestParam("code") String code,
                                       @RequestParam("userPassword") String userPassword,
                                       @RequestParam(value = "rsaSign",required = false) String rsaSign,
                                       @RequestParam(value = "from",required = false,defaultValue = "0") int from)throws Exception {
        Map<String,Object> user = new HashMap<>();
        user.put("mobile",mobile);
        user.put("mobileCode",code);
        user.put("password",userPassword);
        user.put("confirmPwd",userPassword);
        user.put("sign",rsaSign);
        //获取当前IP的地址
        //user.put("regAddress",WebUtils.getIpToAddressStr(request));
        if(from==1){
            user.put("registerFrom", UserExpandFrom.androidFrom.getKey());
        }else if(from==2){
            user.put("registerFrom", UserExpandFrom.iosFrom.getKey());
        }
        Map<String,Object> resultMap =userLoginService.toRegister(user,LoginForm.LOGIN_FORM_APP);
        if(!ResultUtil.getSuccess(resultMap)){//如果登录失败，则重新获取密钥
            Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
            resultMap.put("keyMap",keyMap);
        }
        return resultMap;
    }

    /**
     * 学员用户登录接口
     * @param account 账号
     * @param userPassword 用户密码
     * @return Map<String,Object>
     */
    @RequestMapping("/login")
    public Map<String,Object> login(@RequestParam("account") String account,
                                    @RequestParam("userPassword") String userPassword,
                                    @RequestParam(value = "rsaSign",required = false) String rsaSign) throws Exception{
        Map<String,Object> params=new HashMap<>();
        params.put("loginAccount",account);
        params.put("password",userPassword);
        params.put("sign",rsaSign);
        Map<String,Object> resultMap = userLoginService.userLogin(params, LoginForm.LOGIN_FORM_APP,"false",null,null);
        if(!ResultUtil.getSuccess(resultMap)){
            Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
            resultMap.put("keyMap",keyMap);
        }
        return resultMap;
    }

    /**
     * 获取密钥接口
     * @return Map<String,Object>
     * @throws Exception
     */
    @RequestMapping("/getKeyMap")
    public Map<String,Object> getKeyMap() throws Exception{
        Map<String,String> keyMap = WebRsaUtils.getRSAData(customCacheChannel);
        return ResultUtil.SUCCESS("密钥获取成功",keyMap);
    }

    /**
     * 获取第三方登录开关
     * @return Map<String,Object>
     * @throws Exception
     */
    @RequestMapping("/thirdPartySwitch")
    public Map<String,Object> thirdPartySwitch(){
        TreeMap<String, String> sysConfigContext = sysConfigService.getSysConfigContext(ConfigName.ONOFF);
        Map map = new HashMap();
        map.put("WX",sysConfigContext.get("weiXinLoginSwitch"));
        map.put("QQ",sysConfigContext.get("qqLoginSwitch"));
        return ResultUtil.SUCCESS("获取成功",map);
    }




}
